﻿using Cl.Utils.Primitives.Class.UtilResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils.Helper
{
    public static class LoadingHelper
    {
        public static void ShowLoading()
        {

        }

        public static void HideLoading()
        {

        }

        public static async Task<UtilResultModel> RunWithLoading(Func<Task<UtilResultModel>> action)
        {
            ShowLoading();
            try
            {
                return await action();
            }
            catch
            {
                HideLoading();
                return new UtilResultModel() { IsSuccess = false };
            }
            finally
            {
                HideLoading();
            }
        }

        public static async Task RunWithLoading(Func<Task> action)
        {
            ShowLoading();
            try
            {
                await action();
            }
            catch
            {
                HideLoading();
            }
            finally
            {
                HideLoading();
            }
        }

        public static async void UseDemo()
        {
            // 模拟异步操作
            await RunWithLoading(async () =>
            {
                try
                {
                    await Task.Delay(3000); // 模拟耗时操作
                    //MessageBox.Show("异步操作完成！");
                }
                catch (Exception ex)
                {
                    //MessageBox.Show($"发生错误: {ex.Message}");
                }
            });
        }
    }
}
